package 数据库操作;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DataBasePool {
    public static void main(String[] args) {
        // 配置HikariCP
        HikariConfig config = new HikariConfig();
        config.setJdbcUrl("jdbc:mysql://localhost:3306/sprintboot-test"); // 数据库URL
        config.setUsername("root"); // 数据库用户名
        config.setPassword("2002"); // 数据库密码
        config.addDataSourceProperty("cachePrepStmts", "true"); // 预编译语句缓存
        config.addDataSourceProperty("prepStmtCacheSize", "250");
        config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");

        // 创建连接池
        HikariDataSource dataSource = new HikariDataSource(config);

        // 从连接池中获取连接
        try (Connection connection = dataSource.getConnection()) {
            // 执行查询
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM user");

            // 处理结果集
            while (resultSet.next()) {
                System.out.println("字段值: " + resultSet.getString("username"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 关闭连接池
        dataSource.close();
    }


}
